<template>
  <!-- onemap -->
  <div :class="['checkmaps ',start_draw_status==true?'draw':'']"
    style="display: flex;height: 100%;min-width: 1446px;position: relative;overflow: hidden;">
    <div class="mapDivs" id="mapDivs">
      <div id="mapDiv1"></div>
    </div>
  </div>
</template>
<script>
console.log(ol)
 const { fromUrl, fromUrls, fromArrayBuffer, fromBlob ,Pool} = GeoTIFF;
  import { renderData } from './renderutils';
  import CanvasTileImageSource, { ProgressBar } from './maputil';
  export default {
    data() {
      return {
        map: null,
      }
    },
    mounted() {
      this.InitMap()
      

    },
    methods: {
      async InitMap() {
        
        this.map = new ol.Map({
          target: "mapDiv1",
          layers: [
            new ol.layer.Tile({
              extent: [-180, -90, 180, 90],
              source: new ol.source.TileWMS({
                url: 'https://s2maps-tiles.eu/wms',
                params: { LAYERS: 's2cloudless-2020' },
                projection: 'EPSG:4326',
              }),
            }),
          ],
          view: new ol.View({
            projection: 'EPSG:4326',
            center: [0, 0],
            zoom: 5,
            minZoom: 3,
            maxZoom: 23,
          }),
        });
        this.sceneLayers = {};
        this.sceneSources = {};
        this.tileCache = {};
        this.renderedTileCache = {};

        //let cogtif_filename_url='https://oin-hotosm.s3.amazonaws.com/56f9b5a963ebf4bc00074e70/0/56f9c2d42b67227a79b4faec.tif'
        let cogtif_filename_url='http://172.16.104.251:9999/xyz/JL1KF01A_PMS01_20210315110406_200044043_101_0001_001_L1_PAN_dtys2.tif'
        let cogtif_ovr=`${cogtif_filename_url}.ovr`


        
        const tiff = await fromUrls(cogtif_filename_url,[cogtif_ovr]);
        const images = [];
        const count = await tiff.getImageCount();
        for (let i = 0; i < count; ++i) {
          images.push(await tiff.getImage(i));
        }
        const first = images[0];
        const resolutions = images.map((image => image.getResolution(first)[0]));
        const tileSizes = images.map((image => [image.getTileWidth(), image.getTileHeight()]));
        // calculate tilegrid from the 'red' image
        const tileGrid = new ol.tilegrid.TileGrid({
          extent: first.getBoundingBox(),
          origin: [first.getOrigin()[0], first.getBoundingBox()[1]],
          resolutions: resolutions.reverse(),
          tileSizes: tileSizes.reverse(),
        });

        
        
        this.pool = new Pool();
        this.canvas = document.createElement('canvas');
        const layer = new ol.layer.Tile({
          source: new CanvasTileImageSource({
            projection: 'EPSG:4326',
            tileGrid,
            tileRenderFunction: (canvas, z, x, y) =>{
              this.renderTile(canvas, z, x, y)
            } ,
          }),
        });
        this.map.addLayer(layer);
      },
      async renderTile(canvas, z, x, y) {
        const id = `${z}-${x}-${y}`;
        console.log(id)
        // if (!this.renderedTileCache[sceneId]) {
        //   this.renderedTileCache[sceneId] = {};
        // }

        // if (!this.renderedTileCache[sceneId][id]) {
        //   this.renderedTileCache[sceneId][id] = this.renderTileInternal(sceneId, canvas, z, x, y);
        // }
        // return this.renderedTileCache[sceneId][id];
      },
    },
    beforeDestroy() {
      document.onkeydown = undefined
      this.$mitt.$off('draw_end_check_back');
      this.$mitt.$off('onmoveend_back');
    },
  }
</script>

<style>
  .checkmaps .mapDivs {
    width: calc(100% - 370px);
    height: 100%;
    position: relative;
    display: flex;
  }

  #task4 {
    width: 370px;
    height: 100%
  }

  .mapDivs #mapDiv1,
  .mapDivs #mapDiv2 {
    width: calc(100% - 0px);
  }

  .standard-map__map-tool {
    position: absolute;
    top: 8px;
    right: 8px;
    z-index: 10;
  }

  .map-tools__item {
    display: inline-block;
    cursor: pointer;
    width: 30px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    -webkit-box-shadow: rgb(0 0 0 / 25%) 0 0 4px 0;
    box-shadow: 0 0 4px 0 rgb(0 0 0 / 25%);
    margin: 0 2px;
    padding: 1px;
    background: hsla(0, 0%, 100%, .92);
    border-radius: 4px;
  }

  .map-tools__item svg {
    height: 28px;
    width: 28px;
  }

  .draw #mapDiv1 {
    /* zoom: 0.5; */
    width: 401px;
    height: 401px;
    position: absolute;
    z-index: 1;
  }

  .draw #mapDiv2 {
    width: 100%;
  }

  .standard-map__mouse-position {
    position: absolute;
    bottom: 8px;
    left: 10px;
    font-size: 12px;
    z-index: 3;
    color: #000000;
    background-color: white;
  }

  #line {
    width: 4px;
    height: 100%;
    position: absolute;
    top: 0;
    left: calc(50% - 5px);
    background-color: #1890ff;
    border: 1px solid #1890ff;
    z-index: 2;
    cursor: pointer;
  }
  .line-ico {
    position: absolute;
    top: 50%;;
    left: -10px;
    width: 24px;
    height: 24px;
    line-height: 24px;
    text-align: center;
    border-radius: 100%;
    color: #fff;
    border: 1px solid #5883f4;
    background-color: #5883f4;
}
</style>